package com.trafficsys.modules.licenseplaterecognition.service.impl;

import com.alipay.api.internal.util.codec.Base64;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trafficsys.modules.entity.Ticket;
import com.trafficsys.modules.licenseplaterecognition.dao.TicketDao;
import com.trafficsys.modules.licenseplaterecognition.service.TicketService;
import com.trafficsys.modules.licenseplaterecognition.vo.TicketVo;
import com.trafficsys.util.LicensePlate;
import com.trafficsys.util.ZhenziSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class TicketServiceImpl implements TicketService {
    @Autowired
    private TicketDao ticketDao;

    @Override
    public HashMap<String, Object> addTicket(Ticket ticket) {
        HashMap<String, Object> map = new HashMap<>();
        //将罚单状态设置为默认状态 未处理
        ticket.setState("未处理");
        int i = ticketDao.insert(ticket);
        if (i > 0) {
            map.put("msg", "SUCCESS");
            //成功插入数据库并且给予对应的司机短信提示
            //根据车牌查询对应的车主的电话号码
            String phoneNum = ticketDao.getPhoneByCarNo(ticket.getCarNo());
            //发送短信
            ZhenziSmsUtil.senSms(phoneNum, ticket.getCarNo());
        } else {
            map.put("msg", "FAILED");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> carNoOCR(String img) {
        HashMap<String, Object> map = new HashMap<>();
        //将base64编码后的图片转为字节流
        byte[] bytes = Base64.decodeBase64String(img);
        String res = LicensePlate.licensePlate(bytes);
        map.put("result", res);
        return map;
    }

    @Override
    public IPage<TicketVo> allTickets(Page<TicketVo> page) {
        return ticketDao.getAllTickets(page);
    }

    @Override
    public IPage<TicketVo> getSelfTickets(Page<TicketVo> page, String card_id) {
        return ticketDao.getSelfTickets(page, card_id);
    }
}
